package com.example.a2023110445

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    
    private lateinit var resultTextView: TextView
    private val REQUEST_CODE_ACTIVITY2 = 100
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        resultTextView = findViewById(R.id.resultTextView)
        
        val button1: Button = findViewById(R.id.button1)
        val button2: Button = findViewById(R.id.button2)
        val button3: Button = findViewById(R.id.button3)
        val button4: Button = findViewById(R.id.button4)
        
        // Button 1: Launch Activity1 with forward parameter
        button1.setOnClickListener {
            val intent = Intent(this, Activity1::class.java)
            intent.putExtra("message", "这是来自MainActivity的消息")
            startActivity(intent)
        }
        
        // Button 2: Launch Activity2 and receive result
        button2.setOnClickListener {
            val intent = Intent(this, Activity2::class.java)
            startActivityForResult(intent, REQUEST_CODE_ACTIVITY2)
        }
        
        // Button 3: Launch Activity3 for camera
        button3.setOnClickListener {
            val intent = Intent(this, Activity3::class.java)
            startActivity(intent)
        }
        
        // Button 4: Launch Activity4 for StopWatch
        button4.setOnClickListener {
            val intent = Intent(this, Activity4::class.java)
            startActivity(intent)
        }
    }
    
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_ACTIVITY2 && resultCode == RESULT_OK) {
            val returnedData = data?.getStringExtra("returnData")
            resultTextView.text = "返回的内容: $returnedData"
        }
    }
}
